﻿using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace SalesManagementCommon.API.SalesManagementCommons.Shippingnotess
{
    public class ShoppingExcelHandler : IRequestHandler<ShoppingExcelCommand, FileExportResult>
    {
        private readonly IBaseRepository<Shippingnote> shippingnoteRepository;

        public ShoppingExcelHandler(IBaseRepository<Shippingnote> shippingnoteRepository)
        {
            this.shippingnoteRepository = shippingnoteRepository;
        }

        public async Task<FileExportResult> Handle(ShoppingExcelCommand request, CancellationToken cancellationToken)
        {
            var result = new FileExportResult();

            var list = await (await shippingnoteRepository.GetAll()).ToListAsync();
            Console.WriteLine(list.Count);
            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet();
            IRow row1 = sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(2).SetCellValue("单据编码");
            row1.CreateCell(3).SetCellValue("单据日期");
            row1.CreateCell(4).SetCellValue("单据状态"); 
            row1.CreateCell(5).SetCellValue("业务状态");
            row1.CreateCell(6).SetCellValue("业务类型");
            row1.CreateCell(7).SetCellValue("仓库");
            row1.CreateCell(8).SetCellValue("客户名称");
            row1.CreateCell(9).SetCellValue("进仓编号");
            row1.CreateCell(10).SetCellValue("交付日期");
            row1.CreateCell(11).SetCellValue("收货人");
            row1.CreateCell(12).SetCellValue("电话");
            row1.CreateCell(13).SetCellValue("地址");
            row1.CreateCell(14).SetCellValue("备注信息");
            row1.CreateCell(15).SetCellValue("运费");
            row1.CreateCell(16).SetCellValue("装卸费");
            row1.CreateCell(17).SetCellValue("备注");



            int rowIndex = 0;
            foreach (var item in list)
            {
                row1 = sheet.CreateRow(rowIndex);
                row1.CreateCell(0).SetCellValue(item.ShippingnoteId);
                row1.CreateCell(2).SetCellValue(item.DocumentNumber);
                row1.CreateCell(3).SetCellValue(item.Documentdate);
                row1.CreateCell(4).SetCellValue(item.DocumentStatus);
                row1.CreateCell(5).SetCellValue(item.BusinessStatus);
                row1.CreateCell(6).SetCellValue(item.BusinessType);
                row1.CreateCell(7).SetCellValue(item.Stash);
                row1.CreateCell(8).SetCellValue(item.Customername);
                row1.CreateCell(9).SetCellValue(item.Warehouseentry);
                row1.CreateCell(10).SetCellValue(item.Deliverydate);
                row1.CreateCell(11).SetCellValue(item.Consignee);
                row1.CreateCell(12).SetCellValue(item.ConsigneePhone);
                row1.CreateCell(13).SetCellValue(item.ConsigneeAddress);
                row1.CreateCell(14).SetCellValue(item.ConsigneeInfo);
                row1.CreateCell(15).SetCellValue(Convert.ToDouble(item.Freight));
                row1.CreateCell(16).SetCellValue(Convert.ToDouble(item.Handlingcharges));
                row1.CreateCell(17).SetCellValue(item.ShippingnoteRemark);

                rowIndex++;

            }
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                data = ms.ToArray();
            }
            string contextType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            string fileName = "发货通知单.xlsx";

            result.FileBytes = data;
            result.MimeType = contextType;
            result.FileName = fileName;
            return result;
        }
    }
}
